package com.lzb.compressmaster.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.FormatAdapter;
import com.lzb.compressmaster.basic.utils.FileP2pUtil;
import com.lzb.compressmaster.bean.CommonBean;
import com.lzb.compressmaster.databinding.ActivityImgFormatBinding;
import com.lzb.compressmaster.utils.ImageFormatConverter;
import com.sztle.admodule.apiprocessor.OutRewardAdApi;
import com.sztle.admodule.apiprocessor.listener.IOutRewardCallback;

import java.util.ArrayList;
import java.util.List;

public class ImgFormatActivity extends BaseAc<ActivityImgFormatBinding> implements View.OnClickListener, OnItemClickListener {

    public static String sPath;
    private FormatAdapter formatAdapter;
    private String format;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_img_format;
    }

    @Override
    protected void initView() {
        mDataBinding.ivImgFormatBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvStartHandle.setOnClickListener(this);

        mDataBinding.rvFormatList.setLayoutManager(new GridLayoutManager(mContext, 3));
        formatAdapter = new FormatAdapter();
        mDataBinding.rvFormatList.setAdapter(formatAdapter);
        formatAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvStartHandle) {//开始处理
            OutRewardAdApi.showRewardAd(this, "picture_format_convert", new IOutRewardCallback() {
                @Override
                public void onRewardEnd() {
                    startConvert();
                }
            });
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        formatAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        formatAdapter.getItem(position).setSelected(true);
        formatAdapter.notifyDataSetChanged();
        format = formatAdapter.getItem(position).getName();
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(sPath).into(mDataBinding.ivImg);
        List<CommonBean> list = new ArrayList<>();
        list.add(new CommonBean(getString(R.string.img_format1), true));
        list.add(new CommonBean(getString(R.string.img_format2), false));
        list.add(new CommonBean(getString(R.string.img_format3), false));
        formatAdapter.setList(list);
        format = list.get(0).getName();
    }

    private void startConvert() {
        showDialog(getString(R.string.save_ing));
        ImageFormatConverter.convertImageAsync(mContext, sPath, format, new ImageFormatConverter.OnConversionListener() {
            @Override
            public void onConversionStart(String inputPath, String outputFormat) {

            }

            @Override
            public void onConversionSuccess(String outputPath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.save_suc));
                FileP2pUtil.copyPrivateImgToPublic(mContext, outputPath);
                startActivity(HomeActivity.class);
            }

            @Override
            public void onConversionError(String errorMessage) {
                dismissDialog();
                ToastUtils.showShort(errorMessage);
            }

            @Override
            public void onConversionProgress(int progress) {

            }
        });
    }


}